<template>
	<!-- <view class="status_bar"></view> -->
	<view class="page-login">
		<view class="page-header">
			<view class="left-title">{{ $t('account.register.sub_title') }}</view>
			<view class="right-btn" @click="back()"><uni-icons type="closeempty" size="28"></uni-icons></view>
		</view>
		<uni-forms :modelValue="data.form" label-width="150" :rules="data.rules" ref="form" class="form"
			label-position="top">
			<uni-forms-item :label="$t('account.login.input_email')" name="email">
				<uni-easyinput placeholderStyle="font-size:14px" :styles="{borderColor: '#ccc'}" type="text"
					v-model="data.form.email" :placeholder="$t('account.register.input_email')" />
			</uni-forms-item>
			<uni-forms-item :label="$t('account.login.t_password')" name="password">
				<uni-easyinput placeholderStyle="font-size:14px" :styles="{borderColor: '#ccc'}" type="password"
					v-model="data.form.password" :placeholder="$t('account.register.input_password')" />
			</uni-forms-item>
			<uni-forms-item :label="$t('account.home.iv_code')" name="sn">
				<uni-easyinput placeholderStyle="font-size:14px" :styles="{borderColor: '#ccc'}" type="text"
					v-model="data.form.sn" :placeholder="$t('account.register.input_iv_code')" />
			</uni-forms-item>
		</uni-forms>

		<view class="bottom-text">
			<text class="forgot-password"></text>
			<view class="register-link" @click="navigateTo('/pages/account/login/login')">
				<text>{{ $t('account.register.login_explain') }}</text>
				<text class="register-text">{{ $t('account.login.t_login') }}</text>
			</view>
		</view>
		<button @click="submit" class="btn-primary btn-login">{{ $t('common.btn_submit') }}</button>
		<view>
			邀请码：{{form.from_code}}
		</view>
		{{plus}}
		<!-- <button @click="handleGetId">获取设置码</button> -->
	</view>
</template>

<script setup>
	import {
		reactive,
		ref,
		getCurrentInstance
	} from 'vue'
	import {
		msg,
		back,
		navigateTo
	} from '@/common/util'
	import {
		useI18n
	} from 'vue-i18n'
	const {
		t
	} = useI18n()
	const {
		proxy
	} = getCurrentInstance()

	const form = ref()

	const data = reactive({
		form: {
			email: '',
			password: '',
			sn: "",
			password_confirmation: '',
			// from_code:"E67AC954277EB039F7F77E321EF66211"
			// #ifdef APP-PLUS
			from_code: plus.device.getDeviceId(),
			// #endif
		},

		rules: {
			email: {
				rules: [{
						required: true,
						errorMessage: t('account.register.input_email'),
						trigger: 'blur'
					},
					{
						format: 'email',
						errorMessage: t('common.input_email_error'),
						trigger: 'blur'
					}
				],
			},
			password: {
				rules: [{
					required: true,
					errorMessage: t('account.register.input_password'),
					trigger: 'blur'
				}],
			},
		},
	})

	const handleGetId = () => {
		// plus.device.getInfo({
		// 	success: function(e) {
		// 		console.log('getDeviceInfo success: ' + JSON.stringify(e),e);
		// 	},
		// 	fail: function(e) {
		// 		console.log('getDeviceInfo failed: ' + JSON.stringify(e));
		// 	}
		// });
	}


	const submit = () => {
		form.value.validate().then(res => {
			data.form.password_confirmation = data.form.password
			console.log(data.form,"提交的参数");
			proxy.$http.post('register', data.form).then(res => {
				// console.log(res,"请求成了吗？");
				uni.setStorageSync('account', res)
				uni.setStorageSync('access-token', res.access_token)
				msg({
					title: t('account.register.register_success'),
					icon: 'success'
				}, () => {
					if (uni.getStorageSync('login_after_url')) {
						proxy.$util.backTo(uni.getStorageSync('login_after_url'))
						return
					}
					uni.switchTab({
						url: '/pages/account/home/home'
					})
				})
			}).catch(err=>{
				console.log("报错",err);
			})
		})
	}

	// 页面卸载
	onUnload(() => {
		uni.removeStorageSync('login_after_url')
	})
</script>

<style lang="scss" scoped>
	@import '@/pages/account/login/login.scss';
</style>